
<template>
    <div class="cloth-application-box">
        <div class="search-cloth not-print">
            <div class="order-item">
                <el-select class="time-type-select" v-model="time_type" style="width: 96px" placeholder="请选择" @change="handleChangeTimeType">
                    <el-option label="申领时间" value="1"></el-option>
                    <el-option label="下单时间" value="2"></el-option>
                </el-select>
                <el-date-picker
                    v-if="time_type=='2'"
                    v-model="searchObj.date"
                    type="date"
                    :picker-options="expireTimeOption"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    style="width:224px;margin-top:4px;margin-left: -5px">
                </el-date-picker>
                <el-date-picker
                    v-if="time_type=='1'"
                    v-model="searchObj.date"
                    type="date"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    style="width:224px;margin-top:4px;margin-left: -5px">
                </el-date-picker>
            </div>
            <!--<div class="order-item">-->
                <!--<el-input v-model="searchObj.key" placeholder="请输入科室名称搜索" style="width:224px;margin-top:4px"></el-input>-->
            <!--</div>-->
            <div class="order-item">
                <el-button class="order-btn"  @click="getLisData"><span>检索</span></el-button>
                <el-button class="order-btn export-btn"  @click="handlExport"><span>导出</span></el-button>
            </div>
        </div>
        <!-- 表格 -->
        <div class="cloth-table not-print" >
            <el-table
                :data="clothList"
                border
                height="100%"
                style="width:100%;"
                @row-click="handleCloth"
                :row-class-name="tableRowClassName">
                <el-table-column
                    label="序号"
                    type="index"
                    width="40"
                    align="center">
                </el-table-column>
                <el-table-column
                    v-for="(v,i) in clothColumns"
                    :key="i"
                    :prop="v.key"
                    :label="v.title"
                    :width="v.width"
                    :class-name="v.class_name"
                    align="center">
                    <template slot-scope="scope">
                        <div v-if="v.title == '确认数量'">
                            {{scope.row.order_status == 1 ? "--" : scope.row.check_total }}
                        </div>
                        <div v-else-if="v.title == '实际送达'">
                            {{scope.row.order_status == 3 ?  scope.row.order_send_total : "--"}}
                        </div>
                        <div v-else>
                            {{scope.row[v.key]}}
                            <span v-if="v.title == '订单状态' && scope.row.is_myself == '1'" class="is-myself">自发单</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 详情 -->
        <order-details
            v-if="detailsModel"
            :row="row"
            :status="order_status"
            dialogTitle="医用织物申领单详情"
            @hideModel="hideOrderModel"
            @onrefresh="handleRefresh"
        ></order-details>

        <el-dialog
            :visible.sync="export_visible"
            title="点击选择科室并导出"
            class="export-dialog"
            width="450px"
        >
            <el-button class="ks-export-item" v-for="(item,index) in ks" :key="index" @click="exoprtWeav(index)">{{item}}</el-button>
        </el-dialog>

        <Export 
            :title="export_lis[0].addr"
            :col="export_col"
            :lis="export_lis"
            :total="lis_total"
            v-if="visible"
            @onclose="visible = false"
        ></Export>
    </div>
</template>

<script>
    import {
        clothLisAxios
    } from '../../server/getData'
    import {timeFormat} from '../../common/today'
    import orderDetails from '../../components/clothing/sub/order-details'
    import Export from './sub/export.vue'
    export default {
        components:{
            orderDetails,
            Export
        },
        data(){
            return{
                time_type:'2',
                expireTimeOption: {
                    disabledDate(date) {
                        return date.getTime() > Date.now();
                    }
                },
                searchObj:{
                    date:timeFormat('','ymd','-'),
                    key:''
                },
                clothList:[],
                clothColumns:[
                    {title:'下单时间',key:'ctime',width:'110',class_name:''},
                    {title:'申领单号',key:'order_sn',width:'150',class_name:''},
                    {title:'申领科室',key:'addr',class_name:''},
                    {title:'预约申领时间',key:'order_time_txt',class_name:'',width:'120'},
                    {title:'下单数量',key:'order_total',class_name:'qty_number',width:'100'},
                    {title:'确认数量',key:'order_check_total',class_name:'check_number',width:'100'},
                    {title:'实际送达',key:'order_send_total',class_name:'send_number',width:'100'},
                    {title:'下单账号',key:'uname',class_name:'',width:'100'},
                    {title:'订单状态',key:'status_text',class_name:'',width:'100'}
                ],
                ztList:[{label:'全部',value:'99'},{label:'待确认',value:'0'},{label:'待打印',value:'1'},{label:'已打印',value:'2'},{label:'已销单',value:'3'}],
                // 申领弹窗
                row:'',
                detailsModel:false,
                order_status:{
                    "1":{title:"待确认",spanClass:"confirm",divClass:"confirm-state",tdClass:"font-gray"},
                    "2":{title:"待送达",spanClass:"complete",divClass:"complete-state",tdClass:"font-gray"},
                    "3":{title:"已送达",spanClass:"sale",divClass:"sale-state",tdClass:""}
                },
                ks:[],
                export_visible: false,
                lis_total:[
                    {type:"下单数量",site:'--',cell_class:"qty-number",total:0},
                    {type:"确认数量",site:'--',cell_class:"check-number",total:0},
                    {type:"实际送达",site:'--',cell_class:"send-number",total:0},
                ],
                export_col:[],
                export_lis:[],
                visible: false,
            }
        },
        
        created(){
            this.getLisData()
        },
        methods:{
            handleChangeTimeType(){
                if(this.time_type == '2'){
//                    this.expireTimeOption = {
//                        disabledDate(date) {
//                            return date.getTime() > Date.now();
//                        }
//                    }
                    //比较时间，大于今天的需要改为今天的日期
                    if(new Date(this.searchObj.date.replace(/-/g,'/')).getTime() > Date.now()){
                        this.searchObj.date = timeFormat('','ymd','-')
                    }
                }
//                else {
//                    this.expireTimeOption = {}
//                    this.$forceUpdate()
//                }

            },

            tableRowClassName({row, rowIndex}){
                if(row.status == '3'){
                    return 'sale-row'
                }
                return ''
            },
            hideOrderModel(){
                this.detailsModel = false
                this.getLisData()
            },
            getLisData(s){
                if(this.searchObj.date == ''){
                    return throwErrorTips('','请选择检索日期')
                }
                clothLisAxios({
                    ctime:this.time_type == 2?this.searchObj.date:'',
                    order_time:this.time_type == 2?'':this.searchObj.date,
                    key:''
                }).then(res=>{
                    this.ks = []

                    this.clothList = res.data.map(v=>{
                        if(!this.ks.includes(v.addr)){
                            this.ks.push(v.addr)
                        }
                        v['order_time_txt'] = v.order_time.replace(' 00:00:00','')
                        v['status_text'] = this.order_status[v.order_status].title
                        v["check_total"] = 0
                        v["send_total"] = 0
                        v.weaving.forEach(wi => {
                            wi["check_total"] = 0
                            wi["send_total"] = 0
                            wi.sizes.forEach(size=>{
                                size['qty_log'] = []
                                size['check_log'] = []
                                size['send_log'] = []
                                size["check"] = size.wsize_check
                                size["send"] = size.wsize_send 
                                if(!size.wsize_check){
                                    size.check = "--"
                                    size.send = "--"
                                }else if(!size.wsize_send){
                                    v.check_total += Number(size.check)
                                     wi.check_total += Number(size.check)
                                    size.send = "--"
                                }else {
                                    v.check_total += Number(size.check)
                                    v.send_total += Number(size.send)
                                    wi.check_total += Number(size.check)
                                    wi.send_total += Number(size.send)
                                }
                                
                            })
                        });
                        if(s && v.id== this.row.id){
                            this.row = v
                        }
                        return v
                    })
                },res=>{})
            },
            handleCloth(row, column, event){
                this.row = row
                this.detailsModel = true
            },
            handleRefresh(){
                this.getLisData(1)
            },
            //
            handlExport(){
                let len = this.ks.length
                if(len == 0){
                    throwErrorTips('','暂无数据导出')
                    return 
                }
                if( len > 1){
                    //选择科室
                    this.export_visible = true
                    return
                }

                exoprtWeav(0)
                
            },
            
            exoprtWeav(index){
                
                this.lis_total = [
                    {type:"下单数量",site:'--',cell_class:"qty-number",total:0},
                    {type:"确认数量",site:'--',cell_class:"check-number",total:0},
                    {type:"实际送达",site:'--',cell_class:"send-number",total:0},
                ]
                let addr = this.ks[index], lis = []
                this.clothList.forEach(v=>{
                    
                    if(v.addr == addr){
                        lis.push(v)
                    }
                })
                console.log(lis);
                let weaving = [], col_obj = {}, col = []
                lis.forEach(v=>{
                    
                    v.weaving.forEach(w => {
                        let wind = weaving.indexOf(w.wname)
                        if(wind == -1){
                            weaving.push(w.wname)    
                            col_obj[w.wname] = {has_data:1}
                            col.push({wname:w.wname,sizes:[]})
                            wind = weaving.length - 1
                        }
                        w.sizes.forEach(s=>{
                            v[w.wname+s.wsize] = {
                                qty: s.wsize_qty ? Number(s.wsize_qty) : "",
                                check: s.wsize_check ? Number(s.wsize_check) : '',
                                send: s.wsize_send ? Number(s.wsize_send) : "",
                                des: s.wsize_des
                            }
                            console.log(v[w.wname+s.wsize]);
                            if(v[w.wname+s.wsize].qty){
                                if(this.lis_total[0][w.wname+s.wsize] == 0 || this.lis_total[0][w.wname+s.wsize]){
                                    this.lis_total[0][w.wname+s.wsize] += v[w.wname+s.wsize].qty
                                }else {
                                    this.lis_total[0][w.wname+s.wsize] = v[w.wname+s.wsize].qty
                                }
                                this.lis_total[0].total += v[w.wname+s.wsize].qty
                            }
                            if(v[w.wname+s.wsize].check){
                                if(this.lis_total[1][w.wname+s.wsize] == 0 || this.lis_total[1][w.wname+s.wsize]){
                                    this.lis_total[1][w.wname+s.wsize] += v[w.wname+s.wsize].check
                                }else {
                                    this.lis_total[1][w.wname+s.wsize] = v[w.wname+s.wsize].check
                                }
                                v["check_total"] += v[w.wname+s.wsize].check
                                this.lis_total[1].total += v[w.wname+s.wsize].check
                            }
                            if(v[w.wname+s.wsize].send){
                                if(this.lis_total[2][w.wname+s.wsize] == 0 || this.lis_total[2][w.wname+s.wsize]){
                                    this.lis_total[2][w.wname+s.wsize] += v[w.wname+s.wsize].send
                                }else {
                                    this.lis_total[2][w.wname+s.wsize] = v[w.wname+s.wsize].send
                                }
                                v["send_total"] += v[w.wname+s.wsize].send
                                this.lis_total[2].total += v[w.wname+s.wsize].send
                            }
                            console.log(col_obj);
                            if(!col_obj[w.wname][s.wsize]){
                                
                                col_obj[w.wname][s.wsize] = 1
                                console.log(wind);
                                col[wind].sizes.push({
                                    wsize:s.wsize
                                })
                            }
                            
                        })
                    })
                })
                lis.sort((a,b)=>{
                    return a.order_time_txt.replace('-','/') - b.order_time_txt.replace('-','/')
                })
                this.export_col = col
                this.export_lis = lis
                this.export_visible = false
                this.visible = true
            }
        }
    }
</script>

<style lang="less">
    @import "../../assets/less/clothLis.less";
</style>